//
//  UIButton+Exit.m
//  OpenDoor
//
//  Created by M&M on 15/6/10.
//  Copyright (c) 2015年 CNMOBI. All rights reserved.
//

#import "UIButton+Exit.h"

@implementation UIButton(UIButton_Exit)

- (void)setIsRegisterAllow:(BOOL)isRegisterAllow
{
    if (isRegisterAllow) {
        self.userInteractionEnabled = YES;
        [self setTitleColor:RGB(117, 65, 3, 1) forState:UIControlStateNormal];
        [self.layer setMasksToBounds:YES];
        [self.layer setCornerRadius:4.0]; //设置矩形四个圆角半径
        [self.layer setBorderWidth:1.0]; //边框宽度
        [self.layer setBorderColor:RGB(244, 208, 5, 1).CGColor];//边框颜色
        [self setBackgroundColor:RGB(255, 225, 35, 1)];
    }
    else
    {
        self.userInteractionEnabled = NO;
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.layer setCornerRadius:4.0];
        [self.layer setBorderWidth:0.0];
        [self setBackgroundColor:RGB(204, 204, 204, 1)];
    }
}
- (BOOL)isRegisterAllow
{
    return self.isRegisterAllow;
}


-(void)startTimeWithTime:(int)time{
    __block int timeout=time; //倒计时时间 /秒
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                
                
                [self.layer setMasksToBounds:YES];
                [self.layer setCornerRadius:4.0]; //设置矩形四个圆角半径
                [self.layer setBorderWidth:1.0]; //边框宽度
                [self.layer setBorderColor:RGB(216, 216, 216, 1).CGColor];//边框颜色
                
                [self setBackgroundColor:[UIColor whiteColor]];
                [self setTitle:@"重新发送" forState:UIControlStateNormal];
                self.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
//                DLog(@"____%@",strTime);
                [self setTitle:[NSString stringWithFormat:@"重新获取验证码(%@)",strTime] forState:UIControlStateNormal];
                self.userInteractionEnabled = NO;
                
            });
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
    
}

@end
